<template>
	<view class="content">
		<u-sticky bgColor="#fff">
			<u-tabs :list="list" :is-scroll="false" style="" :current="current" @change="change"></u-tabs>
		</u-sticky>
		<view v-for="(item, index) in dataList" :key="index" v-show="isAnnounce == 0">
			<u-card :title="item.name" padding="30" margin="26rpx" :sub-title="item.courseName">
				<view class="" slot="body">
					<!-- <u-parse class="u-body-item-title u-line-2" :html="item.content"></u-parse> -->
					<view>开考时间：{{ item.timeRangesta }}</view>
					<view>结束时间：{{ item.timeRangeend }}</view>
					
				</view>
				<view slot="foot" style="display: flex;align-items: center;justify-content: space-between;">
					<view>考试时长：{{ item.duration }}分钟</view>
					<view>
						<view v-if="item.type==0" class="mini-btn"  style="color: #f37b1d;">考试未开始</view>
						<button v-if="item.type==1" class="mini-btn" type="primary" size="mini" @click="publishJob(item)">考试中</button>
						<view v-if="item.type==2" class="mini-btn" style="color: crimson;">考试已结束</view>
					</view>
				</view>
			</u-card>
		</view>

		<view v-for="(item, index) in dataList2" :key="'index'+index" v-show="isAnnounce == 1">
			<u-card :title="item.tpName" padding="30" margin="26rpx" :sub-title="item.examinationTime">
				<view class="" slot="body">
					<view>考试成绩：{{ item.score }}分</view>
				</view>
				<view slot="foot" style="display: flex;align-items: center;justify-content: space-between;">
					<view>考试时间：{{ item.timeConsuming }}秒</view>
					<view>

					</view>
				</view>
			</u-card>
		</view>

	</view>
</template>

<script>
	import {
		getWorkInfoList,
		delWorkJob,
		editWorkJob,
		getStuPaperList
	} from '@/api/jz/class/class.js';

	import {
		getPaperInfoByuserId
	} from '@/api/jz/paper/api.js';

	export default {
		data() {
			return {
				list: [{
					name: '未完成'
				}, {
					name: '考试记录',
				}],
				current: 0,
				swiperCurrent: 0,
				isAnnounce: 0, //0 保存 ； 1 保存并发布
				jobDetailsArr: [], //作业详情
				dataList: [],
				dataList2: []
			}
		},
		mounted() {
			this.change(0)
		},
		methods: {
			publishJob(row) {
				uni.navigateTo({
					url: '/pages/work/myHomework/papering?obj=' + encodeURIComponent(JSON.stringify(row))
				})
			},

			editJob(row) {
				// uni.navigateTo({ url: '/pages/work/workInfo/publishJob?worksObj=' + encodeURIComponent(JSON.stringify(row)) })
			},
			delJob(id) {
				delWorkJob(id).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: `删除成功`
						})
						this.change(this.current)
					}
				}).catch((err) => {
					console.error(err);
				});
			},
			formatTime(time,time2){
				var date = new Date(time);
				var date2 = new Date(time2);
				var  time1  = date.getTime();
				var  time3 = date2.getTime();
				
				if(Date.now()<time1){
					return 0;//未开始考试
				}else if(Date.now()>=time1&&Date.now()<=time3){
					return 1;//考试中
				}else{
					return 2;//考试已经结束
				}
			},
			
			change(index) {
				this.current = index;
				switch (index) {
					case 0:
						this.isAnnounce = 0
						getPaperInfoByuserId().then((res) => {
							if (res.code == 200) {
								this.dataList = res.rows
								for (var i = 0; i < this.dataList.length; i++) {
									this.dataList[i].type = this.formatTime(this.dataList[i].timeRangesta,this.dataList[i].timeRangeend);
									
								}
								
							}
						}).catch((err) => {
							console.error(err);
						});
						break;
					case 1:
						this.isAnnounce = 1
						getStuPaperList().then((res) => {
							if (res.code == 200) {
								this.dataList2 = res.data
							}
						}).catch((err) => {
							console.error(err);
						});
						break;
				}


			},
		},
		onLoad: function() {}
	}
</script>

<style scoped>
	page {
		height: 100%;
		background-color: #f2f2f2;
	}
</style>
<style scoped lang="scss">
	.u-card-wrap {
		background-color: $u-bg-color;
		padding: 1px;
	}

	.u-body-item {
		font-size: 32 rpx;
		color: #333;
		padding: 20 rpx 10 rpx;
	}

	.u-body-item image {
		width: 120 rpx;
		flex: 0 0 120 rpx;
		height: 120 rpx;
		border-radius: 8 rpx;
		margin-left: 12 rpx;
	}
</style>